import numpy 
import os
import pathlib
# GAIA
def for_GAIA_graph(config, global_config, train_case_name: list, test_case_name: list, if_train: bool):
    # 如果是训练的话，单独拼接case_name
    # 如果是检测的话，先拼接训练的case_name即config的failure_type,再去拼接test的case_name

    train_data, train_label = None, None
    test_data, test_label = None, None
    input_entity_embedding_path = pathlib.Path(config['output_data_path_v'])
    pathlib.Path(config['output_data_path_g']).mkdir(exist_ok=True, parents=True)
    if not if_train:
        # 测试
        for index, failure in enumerate(test_case_name):
            data_item = numpy.load(input_entity_embedding_path/failure/f"{failure}.npz")
            print(f"{index}--{failure}--{data_item['y'].tolist()}")
            if test_data is None:
                test_data = data_item['x'].tolist()
                test_label = data_item['y'].tolist()
            else:
                for i in range(len(data_item['x'])):
                    if data_item['y'][i]==1:
                        test_data.append(data_item['x'][i])
                        test_label.append(index+1)
        test_data = numpy.array(test_data)
        test_label = numpy.array(test_label)
        numpy.savez(global_config['graph_embedding']['input_test_data_path'], x = test_data, y=test_label)
        print(f"for_GAIA_graph test_data:{test_data.shape}")
    # 无论是训练还是检测都需要训练数据
    for index, failure in enumerate(train_case_name):
        data_item = numpy.load(input_entity_embedding_path/failure/f"{failure}.npz")
        print(f"{index}--{failure}--{data_item['y'].tolist()}")
        if train_data is None:
            train_data = data_item['x'].tolist()
            train_label = data_item['y'].tolist()
        else:
            for i in range(len(data_item['x'])):
                if data_item['y'][i]==1:
                    train_data.append(data_item['x'][i])
                    train_label.append(index+1)
    train_data = numpy.array(train_data)
    train_label = numpy.array(train_label)
    print(f"for_GAIA_graph train_data: {train_data.shape}")
    numpy.savez(global_config['graph_embedding']['input_train_data_path'], x = train_data, y=train_label)

